<?php

namespace falcon5\inc\WPProviders;

use falcon5\inc\Base\Common;
use falcon5\inc\Base\ServiceProviderInterface;
use falcon5\inc\Helpers\AssetsHelper;

class FrontendScripts implements ServiceProviderInterface
{

    public function register()
    {
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
    }


    function enqueue() {
        $restUrl = trim(falcon5_home_url(), '/');
        $assets = esc_url( AssetsHelper::getAssetUri() );
        
        // 1. 首先加载样式
        wp_enqueue_style( Common::PLUGIN_ID . '-edit', $assets . '/css/falcon5-edit.css' );
        wp_enqueue_style( Common::PLUGIN_ID . '-reveal', $assets . '/libs/reveal/reveal.css' );
        wp_enqueue_style( Common::PLUGIN_ID . '-language-switcher', $assets . '/css/falcon5-language-switcher.css' );
        
        // 2. 然后加载基础脚本
        wp_enqueue_script( Common::PLUGIN_ID . '-monitor', $assets . '/js/falcon5-monitor.js' );
        wp_enqueue_script( Common::PLUGIN_ID . '-reveal', $assets . '/libs/reveal/reveal.js', [], '', true );
        wp_enqueue_script( Common::PLUGIN_ID . '-utils', $assets . '/js/falcon5-utils.js', [], '', true );
        
        // 3. 在加载语言切换器脚本之前，先注入配置
        wp_localize_script(Common::PLUGIN_ID . '-utils', Common::PLUGIN_ID . '_ajax_config', [
            'ajax_url' => falcon5_admin_url( 'admin-ajax.php' ),
            'assetsUri' => $assets,
            'rest_url' => rest_url(),
            'fc5_login_url' => Common::getFc5AdminLoginUrl(),
        ]);
        
        // 4. 最后加载依赖这些配置的脚本
        wp_enqueue_script( Common::PLUGIN_ID . '-language-switcher', $assets . '/js/falcon5-language-switcher.js', [Common::PLUGIN_ID . '-utils'], '', true );
        wp_enqueue_script( Common::PLUGIN_ID . '-edit', $assets . '/js/falcon5-edit.js', [Common::PLUGIN_ID . '-reveal', Common::PLUGIN_ID . '-utils'], '', true );
//        wp_enqueue_script( Common::PLUGIN_ID . '-url-slug', $assets . '/js/falcon5-url-slug.js', [Common::PLUGIN_ID . '-utils'], '', true );
    }
}